import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/index'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: () => import('@/page/index/index.vue'),
  },
  {
    path: '/category',
    component: () => import('@/page/index/category/index.vue')
  },
  {
    path: '/proguct/:id',
    component: () => import('@/page/index/progouct/index.vue'),
  },
  {
    path: '/login',
    component: () => import('@/page/login/index.vue')
  },
  {
    path: '/cart',
    component: () => import('@/page/index/Cart.vue')
  },
  {
    path: '/demo',
    component: () => import('@/views/Layout.vue')
  },
  {
    path: '/city',
    component: () => import('@/views/index.vue')
  },
  {
    path: '/happy',
    name: 'happy',
    meta: { title: '娱乐' },
    component: () => import('@/views/happy.vue')
  }
]

const router = new VueRouter({
  mode: 'hash',  //路由模式   hash history  
  routes,
  store
})
router.beforeEach((to, from, next) => {
    let token = store.state.login.logo.token
  //   console.log(token);
    if (to.path == "/login") {
      if (token) {
  //       console.log("禁止重复登录");
        next(from.path)
      } else {
        next()
      }
    } else {
      if (token) {
        next()
      } else {
        next("/login")
      }
    }
  })
  // router.afterEach((to,from)=>{//全局后置守卫
  //   console.log(to,from);
  // })
export default router
